package com.example.administrator.myapplication;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/*
*
*  TODO:添加商品
* */
    public class AddMerchandiseActivity extends Activity  {
    private TextView goods_name;
    private EditText price;
    private Button sure_to_add;
    private Handler mHandler;
    private Context mContext;
    private ListView mlistView;
    private LinearLayout linearLayout;
    private ScrollView scrollView;
    private AddMerccandiseAdapterActivity adapter;
    private  Button mButton;
    private Double max_price,min_price;
    private long id;
    ArrayList<AddMerchandiseListActivity> mList=new ArrayList<>();
    private void AddMerchandiseActivity(Context context){
        this.mContext=context;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addmerchandise);
        initView();
        httpGetResolve();
        setmHandler();
        goods_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding();
            }
        });


        }
        private void valueSend(final long id){

            sure_to_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SharedPreferences sharedPreferences=getSharedPreferences("data",0);
                    final String access_token=sharedPreferences.getString("access_token",null);

                    if (TextUtils.isEmpty(price.getText().toString())){
                        Toast.makeText(AddMerchandiseActivity.this,"输入的金额不能为空",Toast.LENGTH_LONG).show();
                        price.setText("");
                        price.requestFocus();
                        return;
                    }
                     final Double prices= Double.valueOf(price.getText().toString());
                     if (prices>max_price){
                        Toast.makeText(AddMerchandiseActivity.this,"输入的最大金额不能超过¥20.00",Toast.LENGTH_LONG).show();
                        price.setText("");
                        price.requestFocus();
                        return;
                    }
                    else if (prices<min_price){
                        Toast.makeText(AddMerchandiseActivity.this,"输入的最大金额不能超过¥0.01",Toast.LENGTH_LONG).show();
                        price.setText("");
                        price.requestFocus();
                        return;
                    }
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            HttpContect httpContect=new HttpContect();
                            String rount="api/goods/create-sku";
                            String data="access_token="+access_token+"&goods_id="+id+"&price="+prices;
                             httpContect.send(rount,data);
                            Intent mIntent=new Intent(AddMerchandiseActivity.this,MerchandiseControlActivity.class);
                            startActivity(mIntent);

                        }
                    }).start();

                }
            });

        }
/*
* TODO:控件初始化
* */
    private void initView() {
        goods_name=findViewById(R.id.goods_name);
        price=findViewById(R.id.price);
        sure_to_add=findViewById(R.id.sure_to_add);

    }

/*
* TODO:设置点击事件
* */

/*
* TODO:将布局与视图绑定和Dialog绑定视图
* */
private void binding(){
    mlistView=new ListView(this);
    linearLayout=new LinearLayout(this);
    scrollView=new ScrollView(this);
    mButton=new Button(this);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    mlistView.setAdapter(adapter);
    linearLayout.addView(mlistView);
    mButton.setText("取消");
    linearLayout.addView(mButton);
    scrollView.addView(linearLayout);
    final Dialog dialog=new Dialog(this,R.style.ActionSheetDialogStyle);
    dialog.getWindow().setGravity(Gravity.BOTTOM);
    dialog.setTitle("请选择商品口味");
    dialog.setContentView(scrollView);
    dialog.show();
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String string = String.valueOf(adapter.data.get(position).goods_name);
            goods_name.setText(string);
             id=adapter.data.get(position).id;
            dialog.dismiss();
            valueSend(id);


        }
    });


}




/*
* TODO:网络请求
* */
    private void httpGetResolve(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpContect_GetActivity get=new HttpContect_GetActivity();
                String rount="api/goods/goods-list";
                String sendData= String.valueOf(get.send(rount));
                Looper.prepare();
                Message message=new Message();
                message.obj=sendData;
                mHandler.sendMessage(message);
                Looper.loop();
            }
        }).start();
    }
    private void setmHandler(){
        mHandler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                String jsonValueData= (String) msg.obj;
                jsonResolve(jsonValueData);
                super.handleMessage(msg);
            }
        };
    }
    /*
    * TODO:解析数据
    *
    * */
    public void jsonResolve(String jsonValueData){
        adapter=new AddMerccandiseAdapterActivity(this);
    try {
        JSONObject jsonObject=new JSONObject(jsonValueData);
        JSONObject data=jsonObject.getJSONObject("data");
        JSONArray goods=data.getJSONArray("goods");
        for(int i=0;i<=goods.length()-1;i++){
            AddMerchandiseListActivity item=new AddMerchandiseListActivity();
            JSONObject jsonObject1= (JSONObject) goods.get(i);
            int id=jsonObject1.optInt("id");
            String goods_name=jsonObject1.optString("goods_name");
             max_price=jsonObject1.optDouble("max_price");
             min_price=jsonObject1.optDouble("min_price");
            item.setId(id);
            item.setGoods_name(goods_name);
            mList.add(item);

        }

        adapter.setData(mList);
        adapter.notifyDataSetChanged();
    } catch (JSONException e) {
        e.printStackTrace();
    }

}


}

